package com.wude.tools;


import android.text.TextUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class FileUtil {
//	private String game_id;
//	private String game_name;
//	private String game_appid;
	private String promote_id;
	private String promote_account;

	public FileUtil() {
//		game_id = "";
//		game_name = "";
//		game_appid = "";
		promote_id = "";
		promote_account = "";
		String gameStr = getStr();
		if(!TextUtils.isEmpty(gameStr)){
			initJson(gameStr);
		}
	}

//	public String getGameId() {
//		return game_id;
//	}
//
//	public String getGameName() {
//		return game_name;
//	}
//
//	public String getGameAppid() {
//		return game_appid;
//	}

	public String getPromoteId() {
		return promote_id;
	}

	public String getPromoteAccount() {
		return promote_account;
	}

	/**
	 * 初始化
	 * 
	 * @return
	 */
	private void initJson(String jsonStr) {
		try {
			JSONObject js = new JSONObject(jsonStr);
//			game_id = js.getString("game_id");
//			game_name = js.getString("game_name");
//			game_appid = js.getString("game_appid");
			promote_id = js.getString("promote_id");
			promote_account = js.getString("promote_account");
		} catch (JSONException e) {
//			game_id = "";
//			game_name = "";
//			game_appid = "";
			promote_id = "";
			promote_account = "";
		}		
	}

	/**
	 * 返回文件中字符串
	 */
	private String getStr() {
		InputStream is = null;
		BufferedReader reader = null;
		String result = "";
		StringBuilder sb = new StringBuilder();
		String line = "";
		try {
			String ccfpath = "/META-INF/mch.properties";
			is = this.getClass().getResourceAsStream(ccfpath);
			reader = new BufferedReader(new InputStreamReader(is));
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			result = sb.toString();
		} catch (Exception e) {
			result = "";
		} finally {
			try {
				if (is != null) {
					is.close();
				}
			} catch (IOException e) {
			}
		}
		return result;
	}
}
